package com.neusoft.bizcore.activiti.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.activiti.enums.PendingDataStatusEnum;
import com.neusoft.bizcore.activiti.model.PendingData;
import com.neusoft.bizcore.activiti.model.QPendingData;
import com.neusoft.bizcore.activiti.repository.CustomizedPendingDataRepository;
import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedPendingDataRepositoryImpl extends AbstractRepository
        implements CustomizedPendingDataRepository {

    @Override
    public List<PendingData> index(final Searchable searchable) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<PendingData> query =
                this.query().selectFrom(qPendingData);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return PendingData.class;
    }

    @Override
    public Page<PendingData> search(final Pageable pageable, final Searchable searchable) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<PendingData> query = this.query().selectFrom(qPendingData);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QPendingData qPendingData = QPendingData.pendingData;

        if (!StringUtils.isEmpty(searchable.getStrValue("instanceId"))) {
            where.and(super.containsStr(qPendingData.instanceId, searchable, "instanceId"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("nodeId"))) {
            where.and(super.containsStr(qPendingData.nodeId, searchable, "nodeId"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("taskId"))) {
            where.and(super.containsStr(qPendingData.taskId, searchable, "taskId"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("workNum"))) {
            where.and(super.containsStr(qPendingData.workNum, searchable, "workNum"));
        }

        return where;
    }

    @Override
    public List<PendingData> findByInstanceIdAndNodeId(final PendingData dto) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final JPAQuery<PendingData> query =
                this.query().selectFrom(qPendingData);
        query.where(
                qPendingData.instanceId.eq(dto.getInstanceId())
                        .and(qPendingData.nodeId.goe(dto.getNodeId())));
        return query.fetch();
    }

    @Override
    public List<PendingData> findByClazzName(final String clazzName) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final JPAQuery<PendingData> query =
                this.query().selectFrom(qPendingData);
        query.where(
                qPendingData.clazzName.eq(clazzName)
                        .and(qPendingData.status.eq(PendingDataStatusEnum.undeal)))
                .orderBy(qPendingData.updateTime.asc());
        return query.fetch();
    }

    @Override
    public List<PendingData> findByInstanceId(final String instanceId) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final JPAQuery<PendingData> query =
                this.query().selectFrom(qPendingData);
        query.where(
                qPendingData.instanceId.eq(instanceId)
                        .and(qPendingData.status.eq(PendingDataStatusEnum.undeal)))
                .orderBy(qPendingData.updateTime.asc());
        return query.fetch();
    }

    @Override
    public List<PendingData> findByWorkNumAndNodeId(final String workNum, final String nodeId) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final JPAQuery<PendingData> query =
                this.query().selectFrom(qPendingData);
        query.where(
                qPendingData.workNum.eq(workNum)
                        .and(qPendingData.nodeId.goe(nodeId)));
        return query.fetch();
    }

    @Override
    public List<PendingData> findByWorkNum(final String workNum) {
        final QPendingData qPendingData = QPendingData.pendingData;
        final JPAQuery<PendingData> query =
                this.query().selectFrom(qPendingData);
        query.where(
                qPendingData.workNum.eq(workNum)
                        .and(qPendingData.status.eq(PendingDataStatusEnum.undeal)))
                .orderBy(qPendingData.updateTime.asc());
        return query.fetch();
    }

}
